package com.mosai.alignpro.api;

import com.mosai.alignpro.api.services.DiagnosisService;
import com.mosai.alignpro.entity.HttpHeader;
import com.mosai.alignpro.util.LogUtils;

import java.util.Map;

import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.fastjson.FastJsonConverterFactory;

/**
 * Created by Rays on 2020/3/25.
 */
public class HttpUtils {
    private static final String DEFAULT_BASE_URL = "http://yapi.mosainet.com:8114/mock/23/";
    private static final String HEADER_AUTHORIZATION = "Authorization";
    private static final String HEADER_ACCEPT_LANGUAGE = "Accept-Language";
    private static HttpUtils instance;
    private Retrofit retrofit;
    private HttpHeader httpHeader;

    private HttpUtils() {
    }

    public static HttpUtils getInstance() {
        if (instance == null) {
            synchronized (HttpUtils.class) {
                if (instance == null) {
                    instance = new HttpUtils();
                }
            }
        }
        return instance;
    }

    private Retrofit getRetrofit() {
        if (retrofit == null) {
            synchronized (HttpUtils.class) {
                if (retrofit == null) {
                    OkHttpClient client = getOkHttpClient();
                    Retrofit.Builder builder = new Retrofit.Builder();
                    builder.client(client)
                            .baseUrl(getBaseUrl())
                            .addConverterFactory(FastJsonConverterFactory.create())
                            .addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io()));
                    retrofit = builder.build();
                }
            }
        }
        return retrofit;
    }

    private String getBaseUrl() {
        String s = httpHeader == null ? DEFAULT_BASE_URL : httpHeader.getBaseUrl();
        if (s != null && !s.endsWith("/")) {
            s += "/";
        }
        LogUtils.i("getBaseUrl", s);
        return s;
    }

    private OkHttpClient getOkHttpClient() {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient.Builder builder = new OkHttpClient.Builder()
                .addInterceptor(chain -> {
                    Request.Builder request = chain.request().newBuilder();
                    if (httpHeader != null) {
                        request.addHeader(HEADER_AUTHORIZATION, httpHeader.getAuthorization());
                        request.addHeader(HEADER_ACCEPT_LANGUAGE, httpHeader.getAcceptLanguage());
                        Map<String, String> extra = httpHeader.getExtra();
                        if (extra != null) {
                            for (String name : extra.keySet()) {
                                String value = extra.get(name);
                                request.addHeader(name, value == null ? "" : value);
                            }
                        }
                    }
                    return chain.proceed(request.build());
                })
                .addInterceptor(logging);
        return builder.build();
    }

    public void setHttpHeader(HttpHeader httpHeader) {
        this.httpHeader = httpHeader;
    }

    public DiagnosisService getDiagnosisService() {
        return getRetrofit().create(DiagnosisService.class);
    }
}
